<?php
/**
 *
 * PHP versions 5
 *
 * Copyright (C)
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
 * of ERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software
 * Fundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
 *
 * @author Bastian Schröder <bs@chemanet.de>
 * @version 2007-02-10
 * @package rail
 */
class rail_root extends rail_container {

	/**
	 * The rail_container contains all rail_groups for rail_users.
	 *
	 * @var rail_container
	 */
	private $group_container;

	/**
	 * The rail_container contains all rail_user who can login into rail.
	 *
	 * @var rail_container
	 */
	private $user_container;


	/**
	 * Initialization of rail_root.
	 *
	 * @param rail_connector $pAttribute
	 * @param int $pId
	 */
	public function __construct ( $pRailConnector, $pId ) {
		parent::__construct( $pRailConnector, $pId );

		// get group conainer
		$data = $this->rail_connector->send_request( "SELECT `object2` FROM objects_relations WHERE `type`='RAIL_GROUP_CONTAINER' AND `object1`='" . $this->get_id() . "' LIMIT 1" )->fetch_assoc();
		$this->group_container = rail_factory::get_object( $this->rail_connector, $data[ "object2" ] );

		// get user container
		$data = $this->rail_connector->send_request( "SELECT `object2` FROM objects_relations WHERE `type`='RAIL_USER_CONTAINER' AND `object1`='" . $this->get_id() . "' LIMIT 1" )->fetch_assoc();
		$this->user_container = rail_factory::get_object( $this->rail_connector, $data[ "object2" ] );
	}


	/**
	 * Throw an Exception if called, because root is the top level.
	 *
	 * @exception rail_exception
	 */
	public function get_environment () {
		throw new rail_exception( "The root container has no environment!" );
	}


	/**
	 * Get RAIL_GROUP_CONTAINER.
	 *
	 * @exception rail_readpermission_exception
	 * @return rail_container
	 */
	public function get_group_container () {
		// check read permission
		if( !$this->is_readable( $this->rail_connector->get_user() ) )
			throw new rail_readpermission_exception( $this, $this->rail_connector->get_user() );

		return $this->group_container;
	}


	/**
	 * Get Path to this object.
	 *
	 * @return string
	 */
	public function get_path () {
		return $this->cache[ "path" ] = "/";
	}


	/**
	 * Get RAIL_USER_CONTAINER.
	 *
	 * @exception rail_readpermission_exception
	 * @return rail_container
	 */
	public function get_user_container () {
		// check read permission
		 if( !$this->is_readable( $this->rail_connector->get_user() ) )
		 	throw new rail_readpermission_exception( $this, $this->rail_connector->get_user() );

		return $this->user_container;
	}


	/**
	 * Move object to new environment.
	 *
	 * @exception rail_exception
	 * @param rail_object
	 */
	public function move ( $pRailObject ) {
		throw new rail_exception( "The root container can't move!" );
	}

} // End of rail_root
?>